#!/bin/bash


IP=192.168.200.30
PORT=25020
# NEWPORT=8551


function color(){
    if [ $1 == "green" ]; then
        echo -e "\033[32;40m=================== $2 ===================\033[0m"
    elif [ $1 == "red" ]; then
        echo -e "\033[31;40m=================== $2 ===================\033[0m"
    fi
}
# 该函数用于获取JSON中某个键的值
function get_json_value(){
    # example get_json_value "data.json" "name" # 从jaon文件中读取
    # echo $(cat "$1" | grep -Po '(?<="'$2'":")[^"\\]*(?:\\.[^"\\]*)*')
    echo $(echo "$1" | grep -Po '(?<="'$2'":")[^"\\]*(?:\\.[^"\\]*)*')
}

color red "查询节点数 [必须开启admin]"
curl -X POST --data '{"jsonrpc":"2.0","method":"admin_peers","params":[],"id":1}' -H "Content-Type: application/json" http://${IP}:${PORT} | jq

color red "查询区块高度"
curl --request POST \
  --url http://${IP}:${PORT}/ \
  --header 'content-type: application/json' \
  --data '{"id":1,"jsonrpc":"2.0","method":"eth_blockNumber","params":[]}'

color red "解锁账号 [账号只需解锁一次]"
curl --data '{"method":"personal_unlockAccount", "params":["0x16d071a5374281dd27f2c946c08bd01283ec86eb","1", 0], "id":1, "jsonrpc":"2.0"}' -H "Content-Type: application/json" -X POST http://${IP}:${PORT}
# curl --data '{"method":"personal_unlockAccount", "params":["55552351aAE7365552fe98FdeA7A4C7555E678e5","1", 0], "id":1, "jsonrpc":"2.0"}' -H "Content-Type: application/json" -X POST http://127.0.0.1:8545


color red "发起交易"
# 发交易; from地址是复制node0地址
# 从该地址 0xbc43dec69f2198fc6b08502e2d7da2efba38c8ed 转账 到 0xB565a3B4a3553474205aAc1Fa1794D8A3A5A03e4
result_json=`curl --request POST \
  --url http://${IP}:${PORT}/ \
  --header 'content-type: application/json' \
  --data '{"jsonrpc": "2.0","method": "eth_sendTransaction","params": [{"from": "0x16d071a5374281dd27f2c946c08bd01283ec86eb","to": "0xe29ac809039bb9f54935582b484f7d1f386f3de3","value": "0x100"}],"id": 1}'`
echo "=============>result_json=$result_json"

hash=`get_json_value $result_json "result"`

# # hash=get_json_value "$result_json" "result"

echo "==================> 交易hash=$hash"

color red "查询交易详情"
curl -X POST \
  --data "{\"jsonrpc\":\"2.0\",\"method\":\"eth_getTransactionByHash\",\"params\":[\"${hash}\"],\"id\":1}" \
  -H "Content-Type: application/json" \
  http://${IP}:${PORT} | jq






# curl --data '{"method":"personal_unlockAccount", "params":["0x8220f95f8db7bacf4313c2906f2800e537715192","1", 0], "id":1, "jsonrpc":"2.0"}' -H "Content-Type: application/json" -X POST http://127.0.0.1:25048

# curl --data '{"method":"personal_unlockAccount", "params":["0xbc43dec69f2198fc6b08502e2d7da2efba38c8ed","1", 0], "id":1, "jsonrpc":"2.0"}' -H "Content-Type: application/json" -X POST http://127.0.0.1:25048


# curl --data '{"method":"personal_unlockAccount", "params":["0x16d071a5374281dd27f2c946c08bd01283ec86eb","1", 0], "id":1, "jsonrpc":"2.0"}' -H "Content-Type: application/json" -X POST http://127.0.0.1:25048
